﻿using Client.Base;
using UnityEngine;
namespace Client.Hotfix
{
    /// <summary>
    /// 登录模块
    /// </summary>
    [CModule]
    public class LoginModule : IModule
    {
        private int timerId;
        public void Enter(params object[] args)
        {
            this.Log("[Module] - Enter LoginModule...");
            //解析url参数
            string openUrl = Native.Instance.OpenURL();
            //......todo


            //链接websocket
            string wss = "ws://192.168.9.109:8443/ws";
            Api.Instance.Get<EventService>().AddListener(EventCode.Event_ConnectedServer, arg =>
            {
                Api.Instance.Get<TimerService>().DelTimerTask(timerId); //链接成功，取消计时器

                ReqLoginMessage_10001 login = new ReqLoginMessage_10001
                {
                     UserId= 10086,
                     Password ="1111"
                };
                login.Send(10001);
            });

            timerId = Api.Instance.Get<TimerService>().AddTimerTask(3000,
                      t1 =>
                      {
                        //3s内链接不上
                         Api.Instance.Get<NetService>().Disconnect();
                          this.Log("Connect Server TimeOut");
                      },
                      t2 =>
                      {

                      }, 1);

            Api.Instance.Get<NetService>().Connect(wss);
        }



        public void Exit()
        {
            Api.Instance.Get<EventService>().RemoveListenerByClass(this);
        }
    }
}

